//
//  UIColor+WS.m
//  WSBaseKit
//
//  Created by zhangcong on 2020/8/18.
//  Copyright © 2020 zhangcong. All rights reserved.
//

#import "UIColor+WS.h"

CGFloat colorComponentFrom(NSString *string, NSUInteger start, NSUInteger length) {
    NSString *substring = [string substringWithRange:NSMakeRange(start, length)];
    NSString *fullHex = length == 2 ? substring : [NSString stringWithFormat: @"%@%@", substring, substring];
    
    unsigned hexComponent;
    [[NSScanner scannerWithString: fullHex] scanHexInt: &hexComponent];
    return hexComponent / 255.0;
}


@implementation UIColor (WS)
+ (UIColor *)colorWithMacHex:(UInt32)hex{
    return [UIColor colorWithMacHex:hex andAlpha:1];
}
+ (UIColor *)colorWithMacHex:(UInt32)hex andAlpha:(CGFloat)alpha{
    return [UIColor colorWithRed:((hex >> 16) & 0xFF)/255.0
                           green:((hex >> 8) & 0xFF)/255.0
                            blue:(hex & 0xFF)/255.0
                           alpha:alpha];
}
    
+ (UIColor *)colorWithMacHexString:(NSString *)hexString {
    CGFloat alpha, red, blue, green;
    
    NSString *colorString = [[hexString stringByReplacingOccurrencesOfString:@"#" withString:@""] uppercaseString];
    switch ([colorString length]) {
        case 3: // #RGB
        alpha = 1.0f;
        red   = colorComponentFrom(colorString, 0, 1);
        green = colorComponentFrom(colorString, 1, 1);
        blue  = colorComponentFrom(colorString, 2, 1);
        break;
        
        case 4: // #ARGB
        alpha = colorComponentFrom(colorString, 0, 1);
        red   = colorComponentFrom(colorString, 1, 1);
        green = colorComponentFrom(colorString, 2, 1);
        blue  = colorComponentFrom(colorString, 3, 1);
        break;
        
        case 6: // #RRGGBB
        alpha = 1.0f;
        red   = colorComponentFrom(colorString, 0, 2);
        green = colorComponentFrom(colorString, 2, 2);
        blue  = colorComponentFrom(colorString, 4, 2);
        break;
        
        case 8: // #AARRGGBB
        alpha = colorComponentFrom(colorString, 0, 2);
        red   = colorComponentFrom(colorString, 2, 2);
        green = colorComponentFrom(colorString, 4, 2);
        blue  = colorComponentFrom(colorString, 6, 2);
        break;
        
        default:
        return nil;
    }
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}
    
- (NSString *)MacHEXString{
    UIColor* color = self;
    if (CGColorGetNumberOfComponents(color.CGColor) < 4) {
        const CGFloat *components = CGColorGetComponents(color.CGColor);
        color = [UIColor colorWithRed:components[0]
                                green:components[0]
                                 blue:components[0]
                                alpha:components[1]];
    }
    if (CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor)) != kCGColorSpaceModelRGB) {
        return [NSString stringWithFormat:@"#FFFFFF"];
    }
    return [NSString stringWithFormat:@"#%02X%02X%02X", (int)((CGColorGetComponents(color.CGColor))[0]*255.0),
            (int)((CGColorGetComponents(color.CGColor))[1]*255.0),
            (int)((CGColorGetComponents(color.CGColor))[2]*255.0)];
}
    
+ (UIColor *)colorWithMacWholeRed:(CGFloat)red
                            green:(CGFloat)green
                             blue:(CGFloat)blue
                            alpha:(CGFloat)alpha
    {
        return [UIColor colorWithRed:red/255.f
                               green:green/255.f
                                blue:blue/255.f
                               alpha:alpha];
    }
    
+ (UIColor *)colorWithWholeRed:(CGFloat)red
                         green:(CGFloat)green
                          blue:(CGFloat)blue
    {
        return [self colorWithMacWholeRed:red
                                    green:green
                                     blue:blue
                                    alpha:1.0];
    }
    
+ (UIColor *)RandomColor {
    NSInteger aRedValue = arc4random() % 255;
    NSInteger aGreenValue = arc4random() % 255;
    NSInteger aBlueValue = arc4random() % 255;
    UIColor *randColor = [UIColor colorWithRed:aRedValue / 255.0f green:aGreenValue / 255.0f blue:aBlueValue / 255.0f alpha:1.0f];
    return randColor;
}
    
    /**
     *  @brief  渐变颜色
     *
     *  @param c1     开始颜色
     *  @param c2     结束颜色
     *  @param height 渐变高度
     *
     *  @return 渐变颜色
     */
+ (UIColor*)gradientFromColor:(UIColor*)c1 toColor:(UIColor*)c2 withHeight:(int)height
    {
        CGSize size = CGSizeMake(1, height);
        UIGraphicsBeginImageContextWithOptions(size, NO, 0);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
        
        NSArray* colors = [NSArray arrayWithObjects:(id)c1.CGColor, (id)c2.CGColor, nil];
        CGGradientRef gradient = CGGradientCreateWithColors(colorspace, (__bridge CFArrayRef)colors, NULL);
        CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(0, size.height), 0);
        
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        
        CGGradientRelease(gradient);
        CGColorSpaceRelease(colorspace);
        UIGraphicsEndImageContext();
        
        return [UIColor colorWithPatternImage:image];
    }

@end
